package org.spark.java.transformation;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;

import java.util.Arrays;
import java.util.List;

/**
 * 功能描述: 根据key排序
 *
 * @param:
 * @return:
 * @auther: 某人的目光
 * @date: 2020/1/14 22:36
 */
public class SortByKey {
    public static void main(String[] args) {
        SparkConf sparkConf = new SparkConf();
        //设置运行环境
        sparkConf.setMaster("local");
        sparkConf.setAppName("sortByKey");
        JavaSparkContext context = new JavaSparkContext(sparkConf);
        List<Tuple2<Integer, String>> list = Arrays.asList(
                new Tuple2<Integer, String>(70, "南京"),
                new Tuple2<Integer, String>(80, "安徽"),
                new Tuple2<Integer, String>(100, "阜阳"));
        JavaPairRDD<Integer, String> listRdd = context.parallelizePairs(list);
        listRdd.sortByKey(false).foreach(new VoidFunction<Tuple2<Integer, String>>() {
            public void call(Tuple2<Integer, String> t) throws Exception {
                System.out.println(t._1 + ":" + t._2);
            }
        });
    }
}
